package com.zzptc.ygz;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private DefaultListModel dt;
    private JTextArea jta;
    private JTextField jtf_message = new JTextField(30);
    private JTextField jtf_port = new JTextField("9999");
    private JTextField jtf_max = new JTextField("30");
    private JButton jbt_start = new JButton("启动");
    private JButton jbt_stop = new JButton("停止");
    List<Socket> list = new ArrayList<>();
    List<User> listuser = new ArrayList<User>();
    ServerSocket ss;
    public void createUI(){
        JFrame jf = new JFrame("服务器");
        jf.setBounds(200,200,500,300);
        //北边Jpanel
        JPanel northPane = new JPanel();
        northPane.setLayout(new GridLayout(1,6));
        northPane.add(new JLabel("人数上限"));
        northPane.add(jtf_max);
        northPane.add(new JLabel("端口"));
        northPane.add(jtf_port);
        northPane.add(jbt_start);
        jbt_stop.setEnabled(false);
        northPane.add(jbt_stop);
        northPane.setBorder(new TitledBorder("配置信息"));
        //中间Jpanl(左右分栏)
        //左栏
        dt = new DefaultListModel();
        JList jl = new JList(dt);
        JScrollPane leftpane = new JScrollPane(jl);
        leftpane.setBorder(new TitledBorder("在线用户"));
        //右栏
        jta = new JTextArea();
        jta.setEnabled(false);
        JScrollPane reghtpane = new JScrollPane(jta);
        reghtpane.setBorder(new TitledBorder("消息显示区"));
        JSplitPane centPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpane,reghtpane);
        //设置分割条大小位置
        centPane.setDividerSize(20);
        centPane.setDividerLocation(100);
        //南部Jpanel
        JPanel southPanel = new JPanel();
        southPanel.setBorder(new TitledBorder("写消息"));
        southPanel.add(jtf_message,BorderLayout.CENTER );
        jf.setLayout(new BorderLayout());
        jf.add(northPane,BorderLayout.NORTH);
        jf.add(centPane,BorderLayout.CENTER);
        jf.add(southPanel,BorderLayout.SOUTH);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        serverEvent();
    }

    private void serverEvent() {

        jbt_start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    ss = new ServerSocket(9999);

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            while (true) {
                                Socket socket = ss.accept();
                                list.add(socket);
                                InputStream in =socket.getInputStream();
                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                                String str = bufferedReader.readLine();
                                String[] split = str.split("#");
                                String username = split[0];
                                String userip = split[1];
                                String userport =split[2];
                                User user = new User(username,userip,userport);
                                listuser.add(user);
                                dt.addElement(username);
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        while (true) {
                                            try {
                                                InputStream in =socket.getInputStream();
                                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                                                String str = bufferedReader.readLine();
                                                jta.append(username+":"+str+"\n");
                                            } catch (IOException e1) {
                                                e1.printStackTrace();
                                            }
                                        }
                                    }
                                }).start();

                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }).start();

            }
        });

        jtf_message.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER){
                    for(Socket s:list) {
                        try {
                            OutputStream out =s.getOutputStream();
                            PrintWriter pw = new PrintWriter(out);
                            pw.println(jtf_message.getText());
                            pw.flush();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                        jta.append("服务器说："+jtf_message.getText()+"\n");
                        jtf_message.setText("");
                }
            }
        });
    }




    public static void main(String[] args) {
        new Server().createUI();
    }
}
